/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package lime2;

/**
 * Defines a unique identifier for reactions. ReactionIDs are stamped when registration of (strong or weak) reactions 
 * is requested.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class ReactionID extends LimeItemID
{
 // private data members 
 transient private String name;	      // TRANSIENT --> we keep track of "what" should be installed "where", because of
 transient private Location target;   // TRANSIENT --> reconciliation of reactions. These fields are transient since
 transient private Reaction reaction; // TRANSIENT --> reconciliation is based on the weak hashtable of the LimeTSMgr 
                                      //               of >>this<< host and on the strong hashtable of the remote host.
                                      // These variables are null for IDs of strong reactions or strong parts of weak 
                                      // reactions; they are set for weak parts of weak reactions. 
 
 private boolean strong; // records whether the corresponding reaction is "pure strong" (= a pure strong reaction is a
                         // stand-alone strong reaction, not the strong part of a weak reaction) - we must keep track 
                         // of this to prevent a malicious application programmer from using the ReactionID of a weak 
                         // reaction to uninstall its (local) strong part by invoking method "removeStrong"
 
 /**
  * Constructor method.
  *
  * @param creationHost the host (LimeServer) on which this item was created 
  * @param n unique numeric identifier (.. must be unique only on the creationHost)
  * @param name the name of the involved tuplespace
  * @param target the Location on which the reaction will be registered
  * @param reaction the corresponding reaction 
  * @param strong is the corresponding reaction a "pure" strong one ?
  */
 ReactionID(LimeServerID creationHost, long n, String name, Location target, Reaction reaction, boolean strong)
 {
  super(creationHost, n);
  this.name = name;
  this.target = target;
  this.reaction = reaction;
  this.strong = strong;
 }

 /**
  * Tests for equality.
  * 
  * @param o Object
  * @return <code>true</code> if the objects match, <code>false</code> otherwise 
  */ 
 public boolean equals(Object o)
 {
  boolean result = false;	
  if ((o != null) && (o instanceof ReactionID)) 
     {
      ReactionID rid = (ReactionID) o;	
      // only an id for a "pure strong" reaction can match the id of a "pure strong" reaction
      result = (strong == rid.strong) && (getNumericIdentifier() == rid.getNumericIdentifier()) // faster checks first
	           && (getCreationHost().equals(rid.getCreationHost())); 
     }
  return result;
 }
  
 /**
  * Returns the name of the involved tuplespace of this ReactionID.
  * 
  * @return name
  */
 String getName()
 {
  return name;	
 }
 
 /**
  * Returns the target of this ReactionID.
  * 
  * @return target
  */
 Location getTarget()
 {
  return target;	
 }
 
 /**
  * Returns the corresponding reaction of this ReactionID.
  * 
  * @return reaction 
  */
  Reaction getReaction()
  {
   return reaction;	
  }
 
  /**
   * Is this reaction a "pure strong" one ? (.. meaning a stand-alone strong reaction, not the strong part of a weak
   * reaction).
   * 
   * @return <code>true</code> or <code>false</code>
   */
  boolean isPureStrong()
  {
   return strong;	
  }
  
}